package com.leyou.item.controller;


import com.leyou.item.entity.Brand;
import com.leyou.item.entity.Category;
import com.leyou.item.server.BrandService;
import com.leyou.pojo.BrandDTO;
import com.leyou.pojo.CategoryDTO;
import com.leyou.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {


    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "sortBy",required = false)String sortBy,
            @RequestParam(value = "desc",defaultValue = "false")Boolean desc
    ){

        PageResult<BrandDTO> brandDTO = brandService.queryBrandByPage(page,rows,key,sortBy,desc);
        return ResponseEntity.ok(brandDTO);
    }
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brand, @RequestParam("cids")List<Long> ids){
        brandService.saveBrand(brand,ids);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brand, @RequestParam("cids") List<Long> ids) {
        brandService.updateBrand(brand, ids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    @DeleteMapping
    public ResponseEntity<Void> deleteByBrandId(@RequestParam("id")Long id){
        brandService.deleteByBrandId(id);
        return ResponseEntity.ok().build();
    }



    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryByCategoryId(@RequestParam("id") Long id){
       return ResponseEntity.ok(brandService.queryByCategoryId(id));
    }



}
